package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo7 {
    public static void main(String[] args) {
        try (
                Connection conn = DBUtil.getConnection();)
        {
            System.out.println("与数据库连接成功！");
            String sql = "SELECT s.name,s.gender,s.age,s.job,c.name,c.floor\n" +
                         "FROM student s\n" +
                         "JOIN class c ON s.class_id = c.id\n" +
                         "WHERE c.name = '1年级1班'";
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            System.out.println("姓名    性别年龄职位        班级        楼层");
            while(rs.next()){
                String name = rs.getString("s.name");
                String gender = rs.getString("s.gender");
                int age = rs.getInt("s.age");
                String job = rs.getString("s.job");
                String className = rs.getString("c.name");
                int floor = rs.getInt("c.floor");
                System.out.println(name + "\t" + gender + "\t" + age + "\t" + job + "\t" + className + "\t" + floor);
            }





        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
